<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>composed</title>
<link rel="stylesheet" href="../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../boost_asio.html" title="Boost.Asio">
<link rel="up" href="../reference.html" title="Reference">
<link rel="prev" href="completion_signature_of_t.html" title="completion_signature_of_t">
<link rel="next" href="config.html" title="config">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../boost.png"></td>
<td align="center"><a href="../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="completion_signature_of_t.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../boost_asio.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="config.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_asio.reference.composed"></a><a class="link" href="composed.html" title="composed">composed</a>
</h3></div></div></div>
<p>
        <a class="indexterm" name="boost_asio.indexterm.composed"></a>
      </p>
<p>
        Creates an initiation function object that may be used to launch an asynchronous
        operation with a stateful implementation.
      </p>
<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span>
    <span class="keyword">typename</span><span class="special">...</span> <span class="identifier">Signatures</span><span class="special">,</span>
    <span class="keyword">typename</span> <span class="identifier">Implementation</span><span class="special">,</span>
    <span class="keyword">typename</span><span class="special">...</span> <span class="identifier">IoObjectsOrExecutors</span><span class="special">&gt;</span>
<span class="keyword">auto</span> <span class="identifier">composed</span><span class="special">(</span>
    <span class="identifier">Implementation</span> <span class="special">&amp;&amp;</span> <span class="identifier">implementation</span><span class="special">,</span>
    <span class="identifier">IoObjectsOrExecutors</span> <span class="special">&amp;&amp;...</span> <span class="identifier">io_objects_or_executors</span><span class="special">);</span>
</pre>
<p>
        The <code class="computeroutput"><span class="identifier">composed</span></code> function simplifies
        the implementation of composed asynchronous operations automatically by wrapping
        a stateful function object for use as an initiation function object.
      </p>
<h5>
<a name="boost_asio.reference.composed.h0"></a>
        <span class="phrase"><a name="boost_asio.reference.composed.parameters"></a></span><a class="link" href="composed.html#boost_asio.reference.composed.parameters">Parameters</a>
      </h5>
<div class="variablelist">
<p class="title"><b></b></p>
<dl class="variablelist">
<dt><span class="term">implementation</span></dt>
<dd><p>
              A function object that contains the implementation of the composed
              asynchronous operation. The first argument to the function object is
              a non-const reference to the enclosing intermediate completion handler.
              The remaining arguments are any arguments that originate from the completion
              handlers of any asynchronous operations performed by the implementation.
            </p></dd>
<dt><span class="term">io_objects_or_executors</span></dt>
<dd><p>
              Zero or more I/O objects or I/O executors for which outstanding work
              must be maintained.
            </p></dd>
</dl>
</div>
<h5>
<a name="boost_asio.reference.composed.h1"></a>
        <span class="phrase"><a name="boost_asio.reference.composed.per_operation_cancellation"></a></span><a class="link" href="composed.html#boost_asio.reference.composed.per_operation_cancellation">Per-Operation
        Cancellation</a>
      </h5>
<p>
        By default, terminal per-operation cancellation is enabled for composed operations
        that are implemented using <code class="computeroutput"><span class="identifier">composed</span></code>.
        To disable cancellation for the composed operation, or to alter its supported
        cancellation types, call the <code class="computeroutput"><span class="identifier">self</span></code>
        object's <code class="computeroutput"><span class="identifier">reset_cancellation_state</span></code>
        function.
      </p>
<h5>
<a name="boost_asio.reference.composed.h2"></a>
        <span class="phrase"><a name="boost_asio.reference.composed.example_"></a></span><a class="link" href="composed.html#boost_asio.reference.composed.example_">Example:</a>
      </h5>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">async_echo_implementation</span>
<span class="special">{</span>
  <span class="identifier">tcp</span><span class="special">::</span><span class="identifier">socket</span><span class="special">&amp;</span> <span class="identifier">socket_</span><span class="special">;</span>
  <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">mutable_buffer</span> <span class="identifier">buffer_</span><span class="special">;</span>
  <span class="keyword">enum</span> <span class="special">{</span> <span class="identifier">starting</span><span class="special">,</span> <span class="identifier">reading</span><span class="special">,</span> <span class="identifier">writing</span> <span class="special">}</span> <span class="identifier">state_</span><span class="special">;</span>

  <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Self</span><span class="special">&gt;</span>
  <span class="keyword">void</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">Self</span><span class="special">&amp;</span> <span class="identifier">self</span><span class="special">,</span>
      <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span> <span class="identifier">error</span><span class="special">,</span>
      <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">n</span><span class="special">)</span>
  <span class="special">{</span>
    <span class="keyword">switch</span> <span class="special">(</span><span class="identifier">state_</span><span class="special">)</span>
    <span class="special">{</span>
    <span class="keyword">case</span> <span class="identifier">starting</span><span class="special">:</span>
      <span class="identifier">state_</span> <span class="special">=</span> <span class="identifier">reading</span><span class="special">;</span>
      <span class="identifier">socket_</span><span class="special">.</span><span class="identifier">async_read_some</span><span class="special">(</span>
          <span class="identifier">buffer_</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">self</span><span class="special">));</span>
      <span class="keyword">break</span><span class="special">;</span>
    <span class="keyword">case</span> <span class="identifier">reading</span><span class="special">:</span>
      <span class="keyword">if</span> <span class="special">(</span><span class="identifier">error</span><span class="special">)</span>
      <span class="special">{</span>
        <span class="identifier">self</span><span class="special">.</span><span class="identifier">complete</span><span class="special">(</span><span class="identifier">error</span><span class="special">,</span> <span class="number">0</span><span class="special">);</span>
      <span class="special">}</span>
      <span class="keyword">else</span>
      <span class="special">{</span>
        <span class="identifier">state_</span> <span class="special">=</span> <span class="identifier">writing</span><span class="special">;</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">async_write</span><span class="special">(</span><span class="identifier">socket_</span><span class="special">,</span> <span class="identifier">buffer_</span><span class="special">,</span>
            <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">transfer_exactly</span><span class="special">(</span><span class="identifier">n</span><span class="special">),</span>
            <span class="identifier">std</span><span class="special">::</span><span class="identifier">move</span><span class="special">(</span><span class="identifier">self</span><span class="special">));</span>
      <span class="special">}</span>
      <span class="keyword">break</span><span class="special">;</span>
    <span class="keyword">case</span> <span class="identifier">writing</span><span class="special">:</span>
      <span class="identifier">self</span><span class="special">.</span><span class="identifier">complete</span><span class="special">(</span><span class="identifier">error</span><span class="special">,</span> <span class="identifier">n</span><span class="special">);</span>
      <span class="keyword">break</span><span class="special">;</span>
    <span class="special">}</span>
  <span class="special">}</span>
<span class="special">};</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">CompletionToken</span><span class="special">&gt;</span>
<span class="keyword">auto</span> <span class="identifier">async_echo</span><span class="special">(</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">socket</span><span class="special">&amp;</span> <span class="identifier">socket</span><span class="special">,</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">mutable_buffer</span> <span class="identifier">buffer</span><span class="special">,</span>
    <span class="identifier">CompletionToken</span><span class="special">&amp;&amp;</span> <span class="identifier">token</span><span class="special">)</span>
  <span class="special">-&gt;</span> <span class="keyword">decltype</span><span class="special">(</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">async_initiate</span><span class="special">&lt;</span><span class="identifier">CompletionToken</span><span class="special">,</span>
      <span class="keyword">void</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">)&gt;(</span>
        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">composed</span><span class="special">(</span>
          <span class="identifier">async_echo_implementation</span><span class="special">{</span><span class="identifier">socket</span><span class="special">,</span> <span class="identifier">buffer</span><span class="special">,</span>
            <span class="identifier">async_echo_implementation</span><span class="special">::</span><span class="identifier">starting</span><span class="special">},</span> <span class="identifier">socket</span><span class="special">),</span>
        <span class="identifier">token</span><span class="special">))</span>
<span class="special">{</span>
  <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">async_initiate</span><span class="special">&lt;</span><span class="identifier">CompletionToken</span><span class="special">,</span>
    <span class="keyword">void</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">)&gt;(</span>
      <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">composed</span><span class="special">(</span>
        <span class="identifier">async_echo_implementation</span><span class="special">{</span><span class="identifier">socket</span><span class="special">,</span> <span class="identifier">buffer</span><span class="special">,</span>
          <span class="identifier">async_echo_implementation</span><span class="special">::</span><span class="identifier">starting</span><span class="special">},</span> <span class="identifier">socket</span><span class="special">),</span>
      <span class="identifier">token</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">{},</span> <span class="number">0</span><span class="special">);</span>
<span class="special">}</span>
</pre>
<h5>
<a name="boost_asio.reference.composed.h3"></a>
        <span class="phrase"><a name="boost_asio.reference.composed.requirements"></a></span><a class="link" href="composed.html#boost_asio.reference.composed.requirements">Requirements</a>
      </h5>
<p>
        <span class="emphasis"><em>Header: </em></span><code class="literal">boost/asio/composed.hpp</code>
      </p>
<p>
        <span class="emphasis"><em>Convenience header: </em></span><code class="literal">boost/asio.hpp</code>
      </p>
</div>
<div class="copyright-footer">Copyright © 2003-2025 Christopher M.
      Kohlhoff<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="completion_signature_of_t.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../boost_asio.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="config.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
